﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using System.Data;

namespace WaiGaoQiaoWeb.Controllers
{
    public class CardModifyController : Controller
    {
        private CardModifyInfo carinfo = new CardModifyInfo(true);
        public ActionResult CardModifyIndex()
        {
            ViewData["deptName"] = GetdeptName();
            ViewData["GroupDept"] = GetGroupDept("-1");
            return View();
        }

        /// <summary>
        /// 获取班组列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetGroupDept(string depid)
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = DeptDDLInfo.GetGroupDept(depid);
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["groupid"]), ds.Tables[0].Rows[i]["groupname"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取部门列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetdeptName()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = carinfo.GetdeptName();
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["Dep_ID"]), ds.Tables[0].Rows[i]["deptName"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        public ActionResult GetGroupDeptChange(string depid)
        {
            ViewData["GroupDept"] = GetGroupDept(depid);
            return Json(ViewData["GroupDept"]);
        }

        /// <summary>
        /// 获取已发卡人员列表
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetCardModifyList(PageVM pvm)
        {
            string query = string.Empty;
            if (string.IsNullOrEmpty(pvm.query))
            {
                query = " and 1=1";
            }
            else
            {
                string[] param = pvm.query.Split(',');
                string strDep_ID = "";
                string strGroup_ID = "";
                string strEmpNo = "";
                string strCardSerial = " and 1=1 ";
                if (param[2] != "-1")
                {
                    strDep_ID += " and Dep_ID='" + param[2] + "'";
                }
                else
                {
                    strDep_ID += " and 1=1 ";
                }
                if (param[4] != "-1")
                {
                    strGroup_ID += " and GroupIdDept='" + param[4] + "'";
                }
                else
                {
                    strGroup_ID += " and 1=1";
                }
                if (param[0] != "")
                {
                    strEmpNo += " and EmpNo='" + param[0] + "'";
                }
                else
                {
                    strEmpNo += " and 1=1 ";
                }
                if (param[3] != "")
                {
                    strCardSerial = " and CardSerial=" + param[3];
                }
                query = strEmpNo + " and PName like '%" + param[1] + "%'" + strDep_ID + strCardSerial + strGroup_ID;
            }
            IList<CardModifyInfo> carlist = carinfo.SelCardModifyInfo(pvm.rp, pvm.page, query);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = carinfo.GetCardModifyCountWhere(query);
            List<string> v;
            foreach (CardModifyInfo item in carlist)
            {
                #region
                v = new List<string>();
                v.Add(item.PID.ToString());
                v.Add(item.EmpNo);
                v.Add(item.PName);
                v.Add(item.deptName);
                v.Add(item.PersonTypeName);
                v.Add(item.CardSerial.ToString());
                v.Add(item.Balance.ToString());
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.PID.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }

        /// <summary>
        /// 点击改卡按钮执行的方法
        /// </summary>
        /// <param name="supplierNo"></param>
        /// <returns></returns>
        public ActionResult ModifyCard(string id)
        {
            CardModifyVM vm = new CardModifyVM();
            CardModifyInfo info = new CardModifyInfo();
            carinfo = info.GetCardModifyInfoOne(id);
            vm.PID = carinfo.PID;
            vm.EmpNo = carinfo.EmpNo;
            vm.deptName = carinfo.deptName;
            vm.PersonTypeName = carinfo.PersonTypeName;
            vm.PName = carinfo.PName;
            vm.Dep_ID = carinfo.Dep_ID;
            vm.CardSerial = carinfo.CardSerial;
            vm.CardStatus = carinfo.CardStatus;
            vm.WorkStatus = carinfo.WorkStatus;
            vm.Balance = carinfo.Balance;
            vm.ExpiredTime = carinfo.ExpiredTime.ToString("yyyy-MM-dd");
            vm.CardOptDeposit = carinfo.CardOptDeposit;
            vm.EmpNoHidden = carinfo.EmpNo;
            vm.blSubsidyCard = carinfo.blSubsidyCard;
            return View(vm);
        }

        /// <summary>
        /// 保存（给人员改卡）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveCard(CardModifyVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                CardModifyInfo car = new CardModifyInfo()
                {
                    EmpNo = model.EmpNoHidden,
                    CardOptDeposit = Convert.ToInt32(model.CardOptDeposit * 100),
                    ExpiredTime = Convert.ToDateTime(model.ExpiredTime),
                    OperatorID = ((LogOnModel)(System.Web.HttpContext.Current.Session["user"])).LogName,
                    blSubsidyCard = model.blSubsidyCard
                };
                result = carinfo.SaveCard(car);
            }
            return Json(result);
        }
    }
}
